﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChronoScript_Compiler.Language.Parser
{
    public partial class Parser
    {
        public LanguageDefinition.IfStatement ParseIf()
        {
            ReadToken();
            ReadToken();
            LanguageDefinition.IfStatement ifStatement = new LanguageDefinition.IfStatement();
            ifStatement.Comparison = ParseExpression();
            if (!(ReadToken() is Tokens.closeParenthesis))
            {
                Console.WriteLine("Expected ) at " + PeekToken(-1).lineNumber);
            }
            ifStatement.Body = ParseBlock();

            
            while (!(PeekToken(-1) is Tokens.closeCurlyBracket) && PeekToken().ToString().ToLower() != "else")
            {
                while (PeekToken() is Tokens.EOL) ReadToken();

                ifStatement.Body.body.Add(parseLine());
            }
            if (PeekToken().ToString().ToLower() == "else")
            {
                ReadToken();
                ifStatement.Else = new LanguageDefinition.CodeBlock();
                if (PeekToken().ToString() == "if")
                    ifStatement.Else.body.Add(ParseIf());
                else
                {
                    while (!(PeekToken() is Tokens.closeCurlyBracket))
                    {
                        ifStatement.Else.body.Add(parseLine());
                    }
                    ReadToken();
                }
            }

            return ifStatement;
        }
    }
}
